package SMS.controller;

import SMS.model.Res;
import SMS.pojo.Teachers;
import SMS.service.TeacherService;
import SMS.vo.IId;
import SMS.vo.ITeacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@CrossOrigin
@RequestMapping("/teacher")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @GetMapping("/list")
    @ResponseBody
    public Res<List<Object>> list(@RequestParam(value = "currentPage", defaultValue = "1") int currentPage,
                                  @RequestParam(value = "pageSize", defaultValue = "10") int pageSize){
        int totalCount = teacherService.getTotalCount();
        List<ITeacher> teachers = teacherService.getTeachers(currentPage, pageSize);
        // 封装数据
        Map<String, Object> data = new HashMap<>();
        data.put("teachers", teachers);
        data.put("totalCount", totalCount);

        Res<List<Object>> res = new Res(200, "success", data);
        return res;
    }

    @PostMapping("/update")
    @ResponseBody
    public Res update(@RequestBody Teachers teacher){
        int row = teacherService.updateTeacher(teacher);
        if (row == 0){
            return new Res(400, "fail", null);
        }
        return new Res(200, "success", null);
    }

    @PostMapping("/delete")
    @ResponseBody
    public Res delete(@RequestBody IId req){
        int row = teacherService.deleteTeacher(req.id);
        if (row == 0){
            return new Res(400, "fail", null);
        }
        return new Res(200, "success", null);
    }

    @PostMapping("/add")
    @ResponseBody
    public Res add(@RequestBody Teachers teacher){
        System.out.println("controller:"+teacher);
        int row = teacherService.addTeacher(teacher);
        if (row == 0){
            return new Res(400, "fail", null);
        }
        return new Res(200, "success", null);
    }

    // 按关键字（教师号或姓名）查询
    @GetMapping("/search")
    @ResponseBody
    public Res<List<Object>> search(@RequestParam(value = "keyword") String req,
                      @RequestParam(value = "currentPage", defaultValue = "1") int currentPage,
                      @RequestParam(value = "pageSize", defaultValue = "10")int pageSize){
        int totalCount = teacherService.countByNo(req);
        // 通过教师号可以查询到至少一条数据
        if (totalCount != 0){
            // 通过教师号查询
            List<ITeacher> teachers = teacherService.searchTeacherByNo(req, currentPage, pageSize);
            // 封装数据
            Map<String, Object> data = new HashMap<>();
            data.put("teachers", teachers);
            data.put("totalCount", totalCount);

            Res<List<Object>> res = new Res(200, "success", data);
            return res;
        }
        // 通过教师号不能查询到数据，尝试通过姓名查询
        else {
            totalCount = teacherService.countByName(req);
            // 通过教师姓名可以查询到至少一条数据
            if (totalCount != 0){
                List<ITeacher> teachers = teacherService.searchTeacherByName(req, currentPage, pageSize);
                // 封装数据
                Map<String, Object> data = new HashMap<>();
                data.put("teachers", teachers);
                data.put("totalCount", totalCount);

                Res<List<Object>> res = new Res(200, "success", data);
                return res;
            }
            // 尝试通过编号或姓名查询都失败
            else {
                return new Res(400, "fail", null);
            }
        }

    }
}
